rm(list = ls())


# Set Working Directory to Replication Folder
taskdir 		<- "~/Dropbox/final_hai_perlman_replication/"
surveydir 		<- paste0(projectdir, "Data/surveys/")


setwd(taskdir)
library(tidyverse)
library(lfe)
require(data.table)
library(stringi)
library(vtable)

## load survey 1 & survey 2
survey1 <- read_csv(paste0(taskdir, 'input/survey1.csv'))
survey2 <- read_csv(paste0(taskdir, 'input/survey2.csv'))



# income factors
income_level_df<- tibble(
					income = 1:24,
					income_fct = c('Less than $14,999',
					 '$15,000 to $19,999',
					 '$20,000 to $24,999',
					 '$25,000 to $29,999',
					 '$30,000 to $34,999',
					 '$35,000 to $39,999',
					 '$40,000 to $44,999',
					 '$45,000 to $49,999',
					 '$50,000 to $54,999',
						 '$55,000 to $59,999',
						 '$60,000 to $64,999',
						 '$65,000 to $69,999',
						 '$70,000 to $74,999',
						 '$75,000 to $79,999',
						 '$80,000 to $84,999',
						 '$85,000 to $89,999',
						 '$90,000 to $94,999',
						 '$95,000 to $99,999',
						 '$100,000 to $124,999',
						 '$125,000 to $149,999',
						 '$150,000 to $174,999',
						 '$175,000 to $199,999',
						 '$200,000 to $249,999',
						 '$250,000 and above'))
income_levels <- income_level_df$income_fct
survey1 <- survey1%>%left_join(income_level_df)
survey2 <- survey2%>%left_join(income_level_df)
survey1$income_fct <- factor(survey1$income_fct,levels = income_levels)
survey2$income_fct <- factor(survey2$income_fct,levels = income_levels)
survey1$wave <- 'Survey 1'
survey2$wave <- 'Survey 2'


# Define Variables to Summarize
tokeep <- c('wave', 'republican',
					'democrat',
					'female',  
					'white', 'college',
					'lost_home', 'know_someone',
					'income_fct')
# Create and Save Table
bind_rows(survey1%>%dplyr::select(tokeep), 
		  survey2%>%dplyr::select(tokeep))%>%
	mutate(
				# political_party = as.factor(political_party),
				independent = ifelse(republican+democrat==0,1,0)
				)%>%
	sumtable(group = 'wave',
			vars = c('republican',
					'democrat', 'independent',
					'female',  
					'white', 'college',
					'lost_home', 'know_someone',
					'income_fct'),
			labels = c('Republican',
					'Democrat', 'Independent',
					'Female',  
					'White', 'Attended College',
					'Lost Home Due to Wildfires', 
					'Know Someone Who Lost Home Due to Wildfires',
					'Household Income'),
			summ = c('notNA(x)','mean(x)','sd(x)'),
			out = 'latex',
			file = paste0(taskdir,'output/descriptive_tables/sumstats_demographics.tex'))
























